<template>
  <div>
    <div class="search">
      <el-form :inline="true" :model="queryParams" size="small">
        <el-form-item label="年级">
          <el-select value-key="id" filterable clearable size="small" v-model="queryParams.gradeNo" placeholder="请选择"
            @change="handleChangeGrade">
            <el-option v-for="item in gradeOptions" :key="item.id" :label="item.name" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级">
          <el-select filterable clearable size="small" v-model="queryParams.classNo" placeholder="请选择"
            @change="handleChangeClass">
            <el-option v-for="item in classOptions" :key="item.classNo" :label="item.name" :value="item.classNo">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="queryParams.userName" @blur="handleSearch" size="small" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查 询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <acc-table height='400px' :tableData="tableData" :columnDefs="columnDefs">
      <template #order={index}>
        <span>{{ index | orderIndex(pageVo) }}</span>
      </template>
      <template #idCardNo={row}>
          <span>{{isShow && row.idCardNo.length === 18 ? $options.filters.idCarDesensitization(row.idCardNo) :  row.idCardNo}}</span>
        </template>
      <template #action={row}>
        <el-button type="text" size="mini" @click="handleRemove(row)">移除</el-button>
      </template>
    </acc-table>
    <acc-page :total='total' :page-size='pageVo.pageSize' :current-page='pageVo.pageNum'
      @current-change='handleChangePage' @size-change='handleChangeSize'></acc-page>
  </div>
</template>

<script>
import { decrypt } from '@/utils/crypto'
import { columnDefs } from './mock'
import { getGradeList, getclassesList } from '@/views/children/antiepidemic/js/base'
import { queryWhiteListByPage, deleteWhiteList } from '@/views/children/antiepidemic/js/preventionRiskScreen'
export default {
  props: {
    currentCampus: String,
    updatedTime: String,
    isShow: Boolean
  },
  data () {
    return {
      visible: false,
      queryParams: {
        gradeNo: {},
        classNo: null,
        userName: null
      },
      columnDefs: [],
      tableData: [],
      pageVo: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      gradeOptions: [],
      classOptions: [],
      currentHeaderSelect: []
    }
  },
  created () {
    this.columnDefs = columnDefs
    this.getGrade()
    this.handleSearch()
  },
  methods: {
    getGrade () {
      getGradeList({ pageNum: 1, pageSize: 999, status: 1 }).then(res => {
        if (res.code !== 0) return
        this.gradeOptions = res.data.records
      })
    },
    getclasses () {
      getclassesList({ gradeName: this.queryParams.gradeNo.name, status: 1, pageNum: 1, pageSize: 999 }).then(res => {
        if (res.code !== 0) return
        this.classOptions = res.data.classList.records
      })
    },
    handleChangeGrade (val) {
      if (val === '') {
        this.queryParams.classNo = null
      }
      if (val !== '') this.getclasses()
      this.handleSearch()
    },
    handleChangeClass () {
      this.handleSearch()
    },
    handleSearch () {
      this.visible = false
      this.pageVo.pageNum = 1
      this.loadDataList()
    },
    loadDataList () {
      queryWhiteListByPage(this.formData()).then(res => {
        res.data = JSON.parse(decrypt(res.data))
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    handleChangePage (page) {
      this.pageVo.pageNum = page
      this.loadDataList()
    },
    handleChangeSize (size) {
      this.pageVo.pageSize = size
      this.handleSearch()
    },
    formData () {
      let temp = {}
      Object.assign(temp, this.queryParams, this.pageVo)
      // let userInfo = localStorage.getItem('Standard__User-Info')
      // temp.tenantNo = JSON.parse(userInfo).value.tenantNo
      temp.userType = 2
      temp.gradeNo = temp?.gradeNo?.no || null
      // if (temp.gradeNo === null) delete temp.gradeNo
      return temp
    },
    handleRemove (row) {
      this.$confirm('是否移除白名单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteWhiteList(row.id).then(() => {
          this.$message.success('移除白名单成功')
          this.loadDataList()
        })
      }).catch(() => { })
    }
  }
}
</script>

<style lang='scss' scoped>
.search {
  display: flex;
  justify-content: space-between;
}
</style>
